// 自定义react hooks
import React, { useState, useEffect, useCallback } from 'react';

// 处理useContext的值为promise的情况
export function usePromise(promise, Default=null) {

    if (!(promise instanceof Promise)) {
        console.error("无法处理非Promise对象！");
        return Default;
    }
    console.log("====>>> usePromise is run !", promise)
    const [content, setContent] = useState(Default);

    // const waitPromise = useCallback(async (promise) => {
    //     let content = await promise;
    //     setContent(content);
    // }, []);

    const waitPromise = async (promise) => {
        let content = await promise;
        content && setContent(content);
    }

    useEffect(() => {
        waitPromise(promise);
    }, []);

    return content;
}